﻿

var g_Root_Page;

function getUrlArgs() {
    var url = location.search; //获取url中"?"符后的字串 
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);

        }
    }
    return theRequest;
}

function msgDialog(title, msg, width, height) {
    
    var w= width ==undefined?220:width;
    var h=height == undefined?40:height;

    art.dialog({
        title: title,
        content: msg,
        lock: false,
        width: w,
        height:h,
        initFn: function () {
        },
        button: [{ name: '确定', focus: true }]
    })
}

function getPropertyByPath(obj, path) {
    
    var paths = path.split('.');
    var r = obj;

    for (var i = 0; i < paths.length; i++) {
        var s = paths[i];
        r = r[s];
    }

    return r;
}
//读取所有部门
function gLoadAllDepts(callBack, async) {
    var a = async != undefined ? async : true;
    $.ajax({
        url: "/System/LoadOrganizationList",
        type: "POST",
        dataType: "json",
        async:false,
        success: function (req) {
            var orgs = JSON.parse(req.Data);
            callBack(orgs);
        }
    });
}

//读取所有案件分类
function gLoadAllCaseCategories(callBack) {
    $.ajax({
        url: "/BaseData/LoadCaseCategoryList",
        type: "POST",
        dataType: "json",
        success: function (req) {
            var orgs = JSON.parse(req.Data);
            callBack(orgs);
        }
    });
}
//获取当期登录用户
function gGetLoginUser() {

    var user;
    $.ajax({
        url: "/Home/GetLoginUser",
        type: "POST",
        dataType: "json",
        async: false,
        success: function (req) {
            if (req.IsSuccess) {
                var u = JSON.parse(req.Data);
                user=u;
            }
        }
    });
    return user;
}
//获取服务器端时间
function gGetCurrentTime() {
    var dtime;
    $.ajax({
        url: "/Home/GetCurrentTime",
        type: "POST",
        dataType: "json",
        async:false,
        success: function (req) {
            if (req.IsSuccess) {
                dtime = req.Data;
            }
        }
    });
    return dtime;
}
//格式化部门名称
function gFormatDeptName(deptName) {
    var index = deptName.indexOf('青羊区分局');

    if (index == 0) {
        return deptName.substr(5);
    } else {
        return deptName;
    }
}

function gFormatDeptNameForColumn(value, rowData, index) {
    return gFormatDeptName(value);
}

//读取部门下属所有的职员
function gLoadUserListByDeptId(deptId) {
    var users;
    $.ajax({
        url: "/System/LoadUserListByDeptId?deptId="+deptId,
        type: "POST",
        dataType: "json",
        async: false,
        success: function (req) {
            if (req.IsSuccess) {
                users = JSON.parse(req.Data);
            }
        }
    });

    return users;
}
//读取所有的物品类型
function gLoadEvidenceTypes() {
    var types;
    $.ajax({
        url: "/BaseData/LoadEvidenceTypeList",
        type: "POST",
        dataType: "json",
        async: false,
        success: function (req) {
            if (req.IsSuccess) {
                types = JSON.parse(req.Data);
            }
        }
    });

    return types;
}
//读取所有计量单位
function gLoadUMs() {
    var ums;
    $.ajax({
        url: "/BaseData/LoadUMList",
        type: "POST",
        dataType: "json",
        async: false,
        success: function (req) {
            if (req.IsSuccess) {
                ums = JSON.parse(req.Data);
            }
        }
    });

    return ums;
}
//读取所有物品来源
function gLoadOrigins() {
    var objs;
    $.ajax({
        url: "/BaseData/LoadOriginList",
        type: "POST",
        dataType: "json",
        async: false,
        success: function (req) {
            if (req.IsSuccess) {
                objs = JSON.parse(req.Data);
            }
        }
    });

    return objs;
}

//C# 日期转化到js
function gConvertDateFromCSharp(date) {
    try
    {
        var d = eval('new ' + eval(date).source); 
        return d;
    } catch (ex) {
        alert(ex);
    }
}
//格式化日期/时间
function gFormatDateFormCSharp(date,fmt) {
    var d = gConvertDateFromCSharp(date);
    if (fmt) {
        return d.toString(fmt);
    } else {
        return d.toString('yyyy-MM-dd');
    }
}
//获取对象的名称 for datagrid
function gGetPropertyName(value, rowData, index) {
    if (value) {
        return value.Name;
    } else {
        return null;
    }
}

//日期格式化
Date.prototype.toString = function (format, loc) {
    var time = {};
    time.Year = this.getFullYear();
    time.TYear = ("" + time.Year).substr(2);
    time.Month = this.getMonth() + 1;
    time.TMonth = time.Month < 10 ? "0" + time.Month : time.Month;
    time.Day = this.getDate();
    time.TDay = time.Day < 10 ? "0" + time.Day : time.Day;
    time.Hour = this.getHours();
    time.THour = time.Hour < 10 ? "0" + time.Hour : time.Hour;
    time.hour = time.Hour < 13 ? time.Hour : time.Hour - 12;
    time.Thour = time.hour < 10 ? "0" + time.hour : time.hour;
    time.Minute = this.getMinutes();
    time.TMinute = time.Minute < 10 ? "0" + time.Minute : time.Minute;
    time.Second = this.getSeconds();
    time.TSecond = time.Second < 10 ? "0" + time.Second : time.Second;
    time.Millisecond = this.getMilliseconds();
    time.Week = this.getDay();

    var MMMArrEn = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    var MMMArr = ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"];
    var WeekArrEn = ["Sun", "Mon", "Tue", "Web", "Thu", "Fri", "Sat"];
    var WeekArr = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];

    var oNumber = time.Millisecond / 1000;

    if (format != undefined && format.replace(/\s/g, "").length > 0) {
        if (loc != undefined && loc == "en") {
            MMMArr = MMMArrEn.slice(0);
            WeekArr = WeekArrEn.slice(0);
        }
        format = format
            .replace(/yyyy/ig, time.Year)
            .replace(/yyy/ig, time.Year)
            .replace(/yy/ig, time.TYear)
            .replace(/y/ig, time.TYear)
            .replace(/MMM/g, MMMArr[time.Month - 1])
            .replace(/MM/g, time.TMonth)
            .replace(/M/g, time.Month)
            .replace(/dd/ig, time.TDay)
            .replace(/d/ig, time.Day)
            .replace(/HH/g, time.THour)
            .replace(/H/g, time.Hour)
            .replace(/hh/g, time.Thour)
            .replace(/h/g, time.hour)
            .replace(/mm/g, time.TMinute)
            .replace(/m/g, time.Minute)
            .replace(/ss/ig, time.TSecond)
            .replace(/s/ig, time.Second)
            .replace(/fff/ig, time.Millisecond)
            .replace(/ff/ig, oNumber.toFixed(2) * 100)
            .replace(/f/ig, oNumber.toFixed(1) * 10)
            .replace(/EEE/g, WeekArr[time.Week]);
    }
    else {
        format = time.Year + "-" + time.Month + "-" + time.Day + " " + time.Hour + ":" + time.Minute + ":" + time.Second;
    }
    return format;
}


function gFindDataById(index, data) {
    for (var i = 0; i < data.length;i++) {
        var d = data[i];
        if (d.Id == index)
            return d;
    }
}

//扩展EasyUI组件
function extendEasyUI() {
    //扩展Combobox
    $.extend($.fn.combobox.methods, {
        getSelected: function (self) {
            var id = $(self).combobox('getValue');
            var datas = $(self).combobox('getData');
            for (var i = 0; i < datas.length; i++) {
                var d = datas[i];
                if (d.Id == id)
                    return d;
            }

        }
    });
}
//通过单据类型获取单据类型名称
function gGetBillTypeName(billType) {
    var name;
    switch (billType) {
        case 1:
            name = "物品记录单";
            break;
        case 2:
            name = "入库单";
            break;
        case 3:
            name = "调用入库单";
            break;
        case 4:
            name = "出库单";
            break;
        case 5:
            name = "调用出库单";
            break;
    }
    return name;
}
//审核状态
function gGetCheckState() {
    return [{ Id: -1, Name: '全部' }, { Id: 0, Name: '未审核' }, { Id: 1, Name: '审核' }];
}

//读取所有出入库类型
function gLoadStockTypes(billType) {
    var objs;
    $.ajax({
        url: "/BaseData/LoadStockTypeList?billType="+billType,
        type: "POST",
        dataType: "json",
        async: false,
        success: function (req) {
            if (req.IsSuccess) {
                objs = JSON.parse(req.Data);
            }
        }
    });

    return objs;
    
    
}

function gUserIsPermission(roleKey) {
    var p;
    $.ajax({
        url: "/Home/CheckPermission?roleKey="+roleKey,
        type: "POST",
        dataType: "json",
        async: false,
        success: function (req) {
            p=req.IsPermission;
        }
    });

    return p;
}

function checkSession() {
    $.ajax({
        url: "/Home/CheckSession",
        type: "POST",
        dataType: "json",
        async: false,
        success: function (req) {
            if (!req.OnLine) {
                var w = window;

                while (w.parent != null) {
                    w = w.parent;
                }
                
                w.location.reload();
            }
        }
    });
}